#ifndef _MODULESMGR_H_
#define _MODULESMGR_H_
#include <QFileInfo>
#include <QString>
#include <QStringList>
#include "CacheMap.h"

class ModuleScope;
class BinaryModule;

enum ItemType
{
    PyFile,
    PyModule
};

/*class Error
{
public:
};*/

class ModulesMgr: public QObject
{
    Q_OBJECT
public:
    static ModulesMgr& instance()
    {
        static ModulesMgr instance;
        return instance;
    }
    virtual ~ModulesMgr();
    ModuleScope* moduleByFileName(const QString& fileName, bool useCache = true);
    ModuleScope* moduleByContent(const QString& content, const QString& fileName);
    ModuleScope* moduleByName(const QString& name, const QString& relPath = "");
    void setProjectPath(const QString& name);
    void setImportPathes(const QStringList& pathes);
private:
    ModulesMgr();
    ModulesMgr(ModulesMgr const&);
    void operator=(ModulesMgr const&);

    ItemType fileType(const QFileInfo& info);
    ModuleScope* parsePyFile(const QFileInfo& info, const QString& name, bool fromBinary = false, bool useCache = true);
    ModuleScope* _parsePyFile(const QString& fileName);

    void saveCache(ModuleScope* scope, const QString& name);
    QString findModuleByName(const QString& importName, const QString& relPath);
    QFileInfo findInPathes(const QString name, const QStringList& pathes);
    QFileInfo findModuleInPathes(const QString name, const QStringList& pathes);
    void setImportPathes();
    BinaryModule *binary();
private:
    CacheMap _cache;
    QStringList _importPathes;
    QString _projectPath;
    BinaryModule* _binary;
    QHash<QString, ModuleScope*> _parsed;
};

#endif
